<?php
class serial_controller_serial extends Base
{

        private $sQuery;

        public function __construct()
        {
                parent:: __construct();
                $this -> sQuery = new serialQuery();
        }
        
        public function serialSeasons()
        {
//            pre($_GET);
            $serialName = $_GET['showTitle'];
            $serialName = str_replace("-", " ", $serialName);
            $seasons = $this -> getSerialSeasons($serialName);
        }

        public function getShow()
        {
            if(isset($_GET['showTitle']) && $_GET['showTitle'] != "" && isset($_GET['sesionNumber']) && $_GET['sesionNumber'] != "" && ctype_digit($_GET['sesionNumber']) && isset($_GET['episodeNumber']) && $_GET['episodeNumber'] != "" && ctype_digit($_GET['episodeNumber']))
            {
//                exit;
                $where = array();
                $where['lower(polish_name)'] = str_replace("-", " ", $_GET['showTitle']);
                $serialData = $this -> common -> select('serials',"*" , $where);
                $serialData[0]['description'] = str_replace('\"', '"', $serialData[0]['description']);
                if(!isset($serialData[0][0]) || !ctype_digit($serialData[0][0]))
                {
                    throw new Exception('Niema takiego serialu', 1);
                }

                $episodeData = $this -> sQuery -> getShowDatails($serialData[0]['id'], $_GET['sesionNumber'], str_replace("-"," ", $_GET['episodeNumber']));
                if(!isset($episodeData[0][0]) || !ctype_digit($episodeData[0][0]))
                {
                    throw new Exception('Niema takiego serialu', 1);
                }
                $episodeData[0]['freeName'] = $this->replace_pl_chars(str_replace(" ", "-", strtolower($serialData[0]['polish_name'])));
                $episodeData[0]['serialName'] = $serialData[0]['polish_name'];
                $where = array();
                $where['serial_id'] = $serialData[0]['id'];

                if(isset($_SESSION[PREFIX]['user']['id']) && $_SESSION[PREFIX]['user']['id'])
                {
                    $lastEpisode = $this -> sQuery -> getLastEpisode($episodeData[0]['id']);
                    if(isset($lastEpisode[0]['id']) && $lastEpisode[0]['id'] != '')
                    {
                       $this -> sQuery -> deleteEpisode($lastEpisode[0]['id']);
                    }
                    
                    $last = array();
                    $last['user_id'] = $_SESSION[PREFIX]['user']['id'];
                    $last['episode_id'] = $episodeData[0]['id'];
                    $this -> common -> save($last, 'last_watch');

//                    $this -> view -> assign('commentsCount', count($comments));
                }

                if(isset($_SESSION[PREFIX]['user']['id']) && $_SESSION[PREFIX]['user']['id']!="")
                {
                    $el['user_id'] = $_SESSION[PREFIX]['user']['id'];
                }
                else
                {
                    $el['user_id'] = '0';
                }
                
                $el['serial_id'] = $serialData[0]['id'];

                $this -> common -> save($el, 'showspopularity');

                $serialsSeasons = $this -> common -> select('shows',"DISTINCT(season)",$where, "ORDER BY season");
                $serials = array();

                foreach($serialsSeasons as $key=>$value)
                {
                    $serials[$value[0]] = $this -> sQuery -> getSeasonSerialsNew($serialData[0]['id'], $value[0]);
                }

                $categories = $this -> sQuery -> getSerialCategories($serialData[0]['id']);
                $comments = $this -> sQuery -> getSerialComments($serialData[0]['id']);

                $where = array();
                $where['serial_id'] = $serialData[0]['id'];
                $news = $this -> common -> select('news', '*', $where);

                $this -> view -> assign('navLv1','Seriale');
                $linkSerial = '<a href="'.SITE_ADDRESS.'serial/'.$episodeData[0]['freeName'].'">'.$serialData[0]['name_to_show'].'</a>';
                $linkEpisode = '<a href="'.SITE_ADDRESS.'serial/'.$episodeData[0]['freeName'].'/s'.$_GET['sesionNumber'].'e'.$_GET['episodeNumber'].'">'.$episodeData[0]['name'].'(s'.$_GET['sesionNumber'].'e'.$_GET['episodeNumber'].')</a>';
                $this -> view -> assign('navLv2',$linkSerial);
                $this -> view -> assign('navLv3',$linkEpisode);

                $this -> title = $serialData[0]['polish_name'].'(s'.$_GET['sesionNumber'].'e'.$_GET['episodeNumber'].') - oglądaj ten odcinek on-line';
//                $this -> title = $serialData[0]['name'].'('.$serialData[0]['polish_name'].')'.' oglądaj odcinek '.$_GET['episodeNumber'].' serii '.$_GET['sesionNumber'].' | shows.pl';
//                pre($serialData[0]['description']);
//                $serialData[0]['description'] = str_replace('<span class="Apple-style-span" style="font-family: \'Trebuchet MS\', Arial, Helvetica, sans-serif; font-size: 12px; ">', "", $serialData[0]['description']);
//                $serialData[0]['description'] = str_replace('<span/>', "", $serialData[0]['description']);
                $this -> description = $serialData[0]['polish_name']."(s".$_GET['sesionNumber']."e".$_GET['episodeNumber'].") - ".$serialData[0]['description'];
                $this -> view -> assign('commentsCount', count($comments));
                $this -> view -> assign('news', $news);
                $this -> view -> assign('comments', $comments);
                $this -> view -> assign('selectedEpisode', $_GET['episodeNumber']);
                $this -> view -> assign('selectedSeason', $_GET['sesionNumber']);
                $this -> view -> assign('serialLinkName', $_GET['showTitle']);
                $this -> view -> assign('serialData', $serialData);
                $this -> view -> assign('episodeData', $episodeData);
                $this -> view -> assign('serialEpisodes', $serials);
                $this -> view -> assign('categories', $categories);
            }
            else
            {

            }
        }

        public function newShows()
        {
            $oldSerials = $this -> sQuery -> getOldserials();
            foreach($oldSerials as $key=>$value)
            {
                $temp = explode("e", $value['odcinek']);
                $season = $temp[0];
                $episode = $temp[1];
                $oldSerials[$key]['season'] = $season;
                $oldSerials[$key]['episode'] = $episode;

                $title = $value['tytulang'];
                if($value['tytulang'] == "")
                {
                    $title = $value['tytulpl'];
                }
                $content = $value['tresc'];
                if($value['tresc'] == "")
                {
                    $content = $value['trescpl'];
                }

                $oldSerials[$key]['content'] = $content;
                $oldSerials[$key]['title'] = $title;
            }
            pre($oldSerials);
        }

        public function getSerialSeasons()
        {
//            exit();
            $where['lower(polish_name)'] = str_replace("-", " ", $_GET['showTitle']);
            $serialData = $this -> common -> select('serials',"*" , $where);
            $serialData[0]['description'] = str_replace('\"', '"', $serialData[0]['description']);
            if(!isset($serialData[0][0]) || !ctype_digit($serialData[0][0]))
            {
                throw new Exception('Niema takiego serialu', 1);
            }

            $where = array();
            $where['serial_id'] = $serialData[0]['id'];

            $serialsSeasons = $this -> common -> select('shows',"DISTINCT(season)",$where, "ORDER BY season");
            $serials = array();

            foreach($serialsSeasons as $key=>$value)
            {
                $serials[$value[0]] = $this -> sQuery -> getSeasonSerialsNew($serialData[0]['id'], $value[0]);
            }

            $categories = $this -> sQuery -> getSerialCategories($serialData[0]['id']);
            $comments = $this -> sQuery -> getSerialComments($serialData[0]['id']);

            $favourites = $this -> sQuery -> getCountofFavourites($serialData[0]['id']);
//            pre($favourites);

            if(isset($_SESSION[PREFIX]['user']['avatar']))
            {
                $this -> view -> assign('userAvatar', $_SESSION[PREFIX]['user']['avatar']);
            }

            $where = array();
            $where['serial_id'] = $serialData[0]['id'];
            $news = $this -> common -> select('news', '*', $where);

            $where = array();
            $where['serial_id'] = $serialData[0]['id'];
            $countNotes = $this -> common -> select('users_notes', 'count(*)', $where);

            $serialData[0]['freeName'] = $this->replace_pl_chars(str_replace(" ", "-", strtolower($serialData[0]['polish_name'])));
            $this -> view -> assign('navLv1','Seriale');
            $linkSerial = '<a href="'.SITE_ADDRESS.'serial/'.$serialData[0]['freeName'].'">'.$serialData[0]['name_to_show'].'</a>';
//
//              $linkEpisode = '<a href="'.SITE_ADDRESS.'serial/'.$episodeData[0]['freeName'].'/s'.$_GET['sesionNumber'].'e'.$_GET['episodeNumber'].'">'.$episodeData[0]['name'].'(s'.$_GET['sesionNumber'].'e'.$_GET['episodeNumber'].')</a>';
            $this -> view -> assign('navLv2',$linkSerial);
            $this -> view -> assign('countNotes', $countNotes);
            $this -> title = $serialData[0]['polish_name'].' - oglądaj ten serial on-line';
            $this -> description = $serialData[0]['description'];
            $this -> view -> assign('commentsCount', count($comments));
            $this -> view -> assign('news', $news);
            $this -> view -> assign('favourites', $favourites);
            $this -> view -> assign('comments', $comments);
            $this -> view -> assign('selectedSeason', 1);
            $this -> view -> assign('serialData', $serialData);
            $this -> view -> assign('serialEpisodes', $serials);
            $this -> view -> assign('categories', $categories);
            $this -> view -> assign('serialLinkName', $_GET['showTitle']);
        }

        public function rewriteold()
        {
            $where = array();
            $where['id'] = $_GET['idserialu'];
            $serial = $this -> common -> select('serials','*',$where);

            $where = array();
            $where['id'] = $_GET['id'];
            $show = $this -> common -> select('shows', '*', $where);

            if(isset($show[0]['id']) && $show[0]['id'] !="")
            {
                $newUrl = str_replace(" ", "-", mb_strtolower($this -> replace_pl_chars($serial[0]['polish_name'])).'/s'.$show[0]['season'].'e'.$show[0]['episode']);
                $this -> FriendlyLink('serial/'.$newUrl);
            }
            else
            {
                $message = array();
                $message['content'] = "Niestety nie posiadamy tego odcinka";
                $message['type'] = "error";
                $this -> setMessage($message);
                $this -> FriendlyLink(SITE_ADDRESS);
            }
        }

        public function comment()
        {
            if(isset($_POST['content']) && $_POST['content'] !="" && isset($_POST['content']) && $_POST['content'] != "")
            {

            }
            else
            {
                
            }
        }


}

?>
